package com.kingdee.pda.ui.one_click_return;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.example.basecommon.http.MyObserver;
import com.example.basecommon.http.RxHelper;
import com.example.basecommon.http.rxjava.BaseResponse;
import com.example.basecommon.http.rxjava.HttpUtils;
import com.example.basecommon.utils.ConstantUtil;
import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.Api;
import com.kingdee.pda.R;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.ui.activity.FastMoveOperateActivity;
import com.kingdee.pda.ui.dialog.RemindDialog;
import com.kingdee.pda.util.CommonUtil;
import com.kingdee.pda.util.WarehouseUtil;

import java.util.HashMap;

import butterknife.BindView;

/**
 * 一键退货
 * Created by dlj
 * DATE 2021/11/30
 * TIME 15:20
 */
public class OneClickReturnActivity extends BaseTitleActivity implements View.OnKeyListener {

    @BindView(R.id.et_snCode)
    EditText et_snCode;

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, OneClickReturnActivity.class));
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {

    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_one_click_return);
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("一键退货");

        setEditTextScannerEvents(et_snCode, this);
        CommonUtil.requestFocusAndNoClearText(et_snCode);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!(v instanceof EditText)) {
            return false;
        }
        EditText editText = (EditText) v;
        String input = editText.getText().toString().trim();
        if (TextUtils.isEmpty(input)) return false;
        if ((keyCode == KeyEvent.KEYCODE_SYSRQ || keyCode == KeyEvent.KEYCODE_ENTER) && event.getAction() == KeyEvent.ACTION_UP) {
            switch (v.getId()) {
                case R.id.et_snCode:
                    //好太太需要截取
                    if (input.length() >= 22) {
                        input = input.substring(0, 22);
                    }
                    scanSnCode(input);

                    break;
            }
        }
        return false;
    }

    //结果处理函数，当从secondActivity中返回时调用此函数
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OneClickReturnResultActivity.REQUEST_CODE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String result = "";
            if (bundle != null) {
                result = bundle.getString("result");
            }
            if ("2".equals(result)) {
                finish();
            } else {
                CommonUtil.requestFocusAndClearText(et_snCode);
            }
        }
    }

    private void scanSnCode(String snCode) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("sn", snCode);
        map.put("warehouseid", WarehouseUtil.getCurrentWarehouse().getId());

        MyObserver<ScanSnResultBean> myObserver = new MyObserver<ScanSnResultBean>(this) {
            @Override
            public void onSuccess(BaseResponse<ScanSnResultBean> result) {
                //如果是扫的机身号流水号，则弹窗
                if (result.data.isFlagStatus()) {
                    RemindDialog mOperateConfirmDialog = new RemindDialog(OneClickReturnActivity.this,Gravity.LEFT);
                    mOperateConfirmDialog.setTitle("操作确认");
                    mOperateConfirmDialog.setContent(getResultContent(result.data));
                    mOperateConfirmDialog.setShowCancelButton();
                    mOperateConfirmDialog.setListener(new RemindDialog.OnConfirmClickListener() {
                        @Override
                        public void onConfirmClick() {
                            scanSnCode(result.data.getSnno());
                        }
                    });
                    mOperateConfirmDialog.show();
                } else {
                    OneClickReturnResultActivity.actionStartForResult(OneClickReturnActivity.this, result);
                }
            }

            @Override
            public void onFailure(BaseResponse result, String msg) {
                SpeakUtils.speakWithToast(OneClickReturnActivity.this, msg, true, false);
                CommonUtil.requestFocusAndClearText(et_snCode);
            }
        };
        HttpUtils.request(Api.class)
                .oneClickReturnScanSn(ConstantUtil.ONECLICK_RETURN, map)
                .compose(RxHelper.observableIO2Main(this))
                .subscribe(myObserver);
    }

    private String getResultContent(ScanSnResultBean data) {
        StringBuffer sb = new StringBuffer();
        sb.append("商品简称：" + data.getSimplenick());
        sb.append("\n");
        sb.append("SKU编码：" + data.getItemskucode());
        sb.append("\n");
        sb.append("SN：" + data.getSnno());
        sb.append("\n");
        sb.append("机身序列号：" + data.getBsnno());
        sb.append("\n");
        sb.append("机身流水号：" + data.getBserialno());
        sb.append("\n");
        sb.append("流水号：" + data.getSerialno());
        sb.append("\n");
        sb.append("状态：" + CommonUtil.getStatusName(data.getSnstatus()));
        return sb.toString();
    }
}
